<template>
  <el-card>
    <template #header>
      <h3>添加用户</h3>
    </template>
    <div>
      <el-form
        :model="accountForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="account">
          <el-input
            type="text"
            v-model="accountForm.account"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="accountForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户权限" prop="checkGroup">
          <el-select v-model="accountForm.checkGroup" placeholder="用户权限">
            <el-option label="普通管理员" value="2"></el-option>
            <el-option label="高级管理员" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确认添加</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
// 引入userAdd
import { userAdd } from '../../api/users.js'
export default {
  data() {
    var validateAccount = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名或账号或手机号'))
      } else {
        callback()
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validateGroup = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择用户权限'))
      } else {
        callback()
      }
    }
    return {
      accountForm: {
        account: '',
        password: '',
        checkGroup: ''
      },
      rules: {
        account: [{ validator: validateAccount, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        checkGroup: [{ validator: validateGroup, trigger: 'change' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // const token = localStorage.getItem('token')
          // const users = localStorage.getItem('users') || '{}'
          // const token = JSON.parse(users).access_token
          const users = JSON.parse(localStorage.getItem('users') || '{}')
          const { code, msg } = await userAdd({
            member_id: users.member_id,
            access_token: users.access_token,
            username: this.accountForm.account,
            password: this.accountForm.password,
            group_id: this.accountForm.checkGroup - 0
          })
          if (code === 1) {
            this.$message.success(msg)
            setTimeout(() => {
              this.$router.push('/users/list')
            }, 1000)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

//
<style lang="less" scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
.el-card {
  height: 100%;
  border: 0;
  .el-form {
    .el-form-item {
      padding: 10px;
    }
  }
  h3 {
    margin-left: 30px;
    font-size: 24px;
    font-weight: normal;
    letter-spacing: 2px;
  }
  .el-input {
    width: 300px;
  }
  .el-select {
    width: 300px;
  }
}
</style>
